#!/bin/bash
#########################################################################
# 配置文件（根据功能修改配置文件）
<<CONFIGURATION
- dtool.sh
    desc: 开发者工具
    - pkg
        desc: 安装管理
        - uninstall
            desc: 卸载，dtool.sh -a 1 -p {product} -d {path} [-f].
            leaf: 1
            func: module_uninstall_manage
            - p {product}:
                mandatory: 1
                desc: 指定产品类型.
            - d {path}:
                mandatory: 1
                desc: 指定软件目录卸载软件包.
            - f:
                desc: 强制卸载模式.
        - install
            desc: 安装.
            leaf: 1
            func: module_install_manage
        - upgrade
            desc: 升级.
            leaf: 1
            func: module_log_collect
    - log
        desc: 日志收集
CONFIGURATION
#########################################################################
# 功能函数（根据功能添加实现函数）
#########################################################################
function module_uninstall_manage()
{
    echo "卸载管理111"
}

function module_install_manage()
{
    echo "-a：安装管理"
    echo "  -a 1：卸载，dtool.sh -a 1 -p {path} [-f]."
    echo "  -a 2：安装."
    echo "  -a 3：升级."
}

function module_log_collect()
{
    echo "log collect module"
}
#########################################################################
# 调用框架（不需要修改）
#########################################################################
current_path=$(pwd)
filename=$(basename "$0")
INPUT_ARGS="dtool.sh "$@

declare -A iter_desc
# set -x

# while getopts "ab" opt; do
#     case $opt in
#         a)
#             DTOOL_FUNC_MODULE="安装管理"
#             ;;
#         b)
#             DTOOL_FUNC_MODULE="日志收集"
#             ;;
#         ?)
#             echo "this is unrecognized parameter."
#             help
#             ;;
#     esac
# done

# if [ -z "$DTOOL_FUNC_MODULE" ]; then
#     help
# fi

function dtool_iter_get_func_attr()
{
    local str=""
    local desc=""
    for ((i=`expr $2+1`;i<=${iter_desc["end"]};i++)); do
        str=$(sed -n $i'p' $filename)
        if [[ "$str" == *"$1"* ]]; then
            desc=${str#*$1': '}
            break
        fi
        if [[ "$str" == *"- "* ]]; then
            break
        fi
    done
    echo "$desc"
}

function dtool_do_func()
{
    eval `echo $1`
}

function main()
{
    iter_desc["start"]=$(cat $current_path/$filename | grep -n "CONFIGURATION" | sed -n '1p')
    iter_desc["start"]=${iter_desc["start"]%\:*}
    iter_desc["end"]=$(cat $current_path/$filename | grep -n "CONFIGURATION" | sed -n '2p')
    iter_desc["end"]=${iter_desc["end"]%\:*}

    cmd_matched=""
    prefix=""
    help=""
    FG_findfail=0
    for ((i=${iter_desc["start"]};i<=${iter_desc["end"]};i++)); do
        local str=$(sed -n $i'p' $filename)
        if [[ "$str" != "$prefix- "* ]]; then
            continue
        fi

        func_name=${str#*"- "}
        func_desc=$(dtool_iter_get_func_attr "desc" $i)
        help="$help$prefix$func_name: $func_desc\n"

        # echo "gkl INPUT_ARGS:$INPUT_ARGS cmd_matched:$cmd_matched func:$func_name desc:$func_desc"
        if [[ "$FG_findfail" == "1" ]]; then
            continue
        fi

        cmd_matched="$cmd_matched$func_name "
        if [[ "$INPUT_ARGS " == "$cmd_matched"* ]]; then
            prefix="    "$prefix
            if [[ "$INPUT_ARGS " == "$cmd_matched" ]]; then
                is_leaf=$(dtool_iter_get_func_attr "leaf" $i)
                if [[ "$is_leaf" == "1" ]]; then
                    dtool_do_func $(dtool_iter_get_func_attr "func" $i)
                    break
                fi
                FG_findfail=1
            fi
        else
            FG_findfail=1
        fi
    done
    if [[ "$FG_findfail" == "1" ]]; then
        echo -e "$help"
    fi
}

main
#########################################################################